package com.his.his_system.controller.VisitController.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.his_system.common.util.RedisCache;
import com.his.his_system.common.util.ResultAdvice;
import com.his.his_system.controller.VisitController.DefaultBean.ChangeJsonAndObject;
import com.his.his_system.controller.VisitController.DefaultBean.Schenduling;
import com.his.his_system.controller.VisitController.app.bean.AliyunReq;
import com.his.his_system.controller.VisitController.app.until.Aliyun;
import com.his.his_system.domain.*;
import com.his.his_system.mapper.DefaultMapper;
import com.his.his_system.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/app/")
public class AppController {
    @Resource
    private DzmPatientService dzmPatientService;

    @Resource
    private DzmHisCareHistoryService dzmHisCareHistoryService;

    @Resource
    private DefaultService defaultService;

    @Resource
    private DefaultMapper defaultMapper;

    @Resource
    private DzmHisHospitalService dzmHisHospitalService;

    @Resource
    private DzmHisDepartmentService dzmHisDepartmentService;

    @Resource
    private  DzmHisRegisteredfeeService dzmHisRegisteredfeeService;

    @Resource
    private RedisCache redisCache;

    @Resource
    private DzmHisCarePkgService dzmHisCarePkgService;

    //登录
    @GetMapping("login")
    public ResultAdvice applogin(String phone,String password){
        QueryWrapper <DzmPatient>
                queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("mobile",phone).eq("password",password);
        DzmPatient user =  dzmPatientService.getOne(queryWrapper);
        if(Objects.isNull(user)){
            return ResultAdvice.fail("登录失败");
        }
        return ResultAdvice.success(user,"登录成功");
    }
    //获取医院信息
    @GetMapping("getHospitalbyid")
    public ResultAdvice getHospitalbyid(){
        QueryWrapper <DzmHisHospital>
                queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",1);
        DzmHisHospital dzmHisHospital = dzmHisHospitalService.getOne(queryWrapper);
       return ResultAdvice.success(dzmHisHospital);
    }
    //获取用户所有病例
    @GetMapping("getallbinlibypid")
    public ResultAdvice getallbinlibypid(Integer pid){
        return ResultAdvice.success(defaultService.getAllHistoryByPatientId(pid),"成功");
    }

    //获取所有科室
    @GetMapping("getalldep")
    public ResultAdvice getalldep(){
        return ResultAdvice.success(dzmHisDepartmentService.list(),"成功");
    }
    /**
     * 获取可预约的的排班信息
     * @return
     */
    @GetMapping("getAllSchedulingByNow")
    public ResultAdvice<List<Schenduling>> getAllSchedulingByNow(Integer did,Integer rid,String name){
        return ResultAdvice.success(defaultService.getAllSchedulingByNow(did,rid,name),"成功");
    }

    /**
     * 获取挂号费用类型
     * @return
     */
    @GetMapping("getregfreelist")
    public ResultAdvice<List<DzmHisRegisteredfee>> getregfreelist(){
        return ResultAdvice.success(dzmHisRegisteredfeeService.list(),"成功");
    }

    /**
     * 获取注册验证码
     */
    @GetMapping("getyanzhengma")
   public ResultAdvice getyanzhengma(String phone){
        try {
            String code = Aliyun.getApi();
            AliyunReq aliyunReq =  ChangeJsonAndObject.jsonChangeObject(Aliyun.getCode(phone,code), AliyunReq.class);
            redisCache.setCacheObject(phone,code,60, TimeUnit.SECONDS);
            return ResultAdvice.success(aliyunReq,"发送成功");
        } catch (Exception e) {
           return ResultAdvice.fail("发送阿里云短信失败");
        }
    }


    @GetMapping("zhuce")
    public  ResultAdvice  zhuce(String phone,String password,String pass,String code){
       Object alicode =  redisCache.getCacheObject(phone);
       if(Objects.isNull(alicode)){
           return ResultAdvice.fail("验证码失效或则号码于验证码不匹配");
       }else {
           if(!(alicode.equals(code))){
               return ResultAdvice.fail("验证码错误");
           }
       }
       //插入数据
       try{
           DzmPatient dzmPatient = new DzmPatient();
           dzmPatient.setHospitalId(1);
           dzmPatient.setName("未设置");
           dzmPatient.setPassword(password);
           dzmPatient.setIsFinal(0);
           dzmPatient.setMobile(phone);
           dzmPatient.setUpdateTime((int)(System.currentTimeMillis()/1000));
           dzmPatient.setCreateTime((int)(System.currentTimeMillis()/1000));
           if(!dzmPatientService.save(dzmPatient)){
               return ResultAdvice.fail("数据库插入失败");
           }
       }catch (Exception e){
           return ResultAdvice.fail("数据库插入失败");
       }
       return ResultAdvice.success("注册成功");
    }

    /**
     * 获取缴费记录
     */
    @GetMapping("paylist")
    public ResultAdvice<List<DzmHisCarePkg>> paylist(String pid){
        try{
            List<Integer> idList = Arrays.asList(0,1);
            QueryWrapper<DzmHisCarePkg> queryWrapper = new QueryWrapper();
            queryWrapper.eq("hospital_id",1)
                    .eq("patient_id",pid)
                    .in("status",idList);
            return  ResultAdvice.success(dzmHisCarePkgService.list(queryWrapper),"成功");
        }catch (Exception e){
            return  ResultAdvice.fail("获取缴费记录失败");
        }
    }


    @GetMapping("getAppRegisterlist")
    public ResultAdvice<List<DzmHisRegistration>> getAppRegisterlist(Integer hid,String pid){
        return ResultAdvice.success(defaultMapper.getAllRegister(hid ,pid),"成功");
    }
}
